import { Application } from "mydog";
import User, { UserAttributes } from "../models/user";
import { DEF } from "./definition/consts";
import { Dictionary, IUidSid } from "./definition/interface";
import { Room } from "./definition/room";

class RoomManager {

  private static _instance: RoomManager | undefined = undefined;
  public static getInstance(): RoomManager {
    if (!this._instance) {
      this._instance = new RoomManager();
    }
    return this._instance;
  }

  private constructor() { }

  private _app: Application = null as any;
  public init(app: Application) {
    this._app = app;
  }

  private _roomID: number = DEF.ROOM.ID_BEGIN;
  private _roomMap: Dictionary<Room> = {};

  private idAutoIncrement() {
    if (this._roomID >= DEF.ROOM.ID_END) {
      this._roomID = DEF.ROOM.ID_BEGIN;
    }
    return ++this._roomID;
  }

  public async onMatchRoom(gameMode: number, uidsid: Array<IUidSid>) {
    const uids = uidsid.map(user => user.uid);
    const playerInfos: Array<{ userInfo: UserAttributes, playerInfo: { sid: string, isRobot: boolean }, gameInfo: any }> = [];

    switch (gameMode) {
      case DEF.GAME.MODE.doudizhu:
        const userInfos: Array<User> | null = await this._app.rpcAwait(DEF.SEVER.ID.DATABASE).database.main.getUsersJoinDoudizhu(uids);
        if (!!userInfos) {
          for (let i = 0; i < userInfos.length; i++) {
            const userInfo = { id: userInfos[i].id, nickname: userInfos[i].nickname } as UserAttributes;
            const playerInfo = { sid: uidsid[i].sid, isRobot: false }; // 理论上顺序是一致的
            const gameInfo = userInfos[i].Doudizhu;
            playerInfos.push({ userInfo, playerInfo, gameInfo });
          }
        }
        break;
      case DEF.GAME.MODE.lushi:
        break;
      default: break;
    }

    const room = new Room(this._app, this.idAutoIncrement(), "", gameMode);
    room.initPlayers(playerInfos);
    room.onMatch();

    this._roomMap[room.id] = room;
    return room.id;
  }

  public onCreateRoom() {

  }

  public onJoinRoom() {

  }

  public onLeaveRoom() {

    // 解绑 rid
    // for (let i = 0; i < users.length; i++) {
    //   this._app.rpc(users[i].sid).connector.main.bindRoomId(users[i].id, roomId);
    // }

  }

  public onPlayerAction(rid: number, uid: number, action: number) {
    let room;
    if (!!rid && rid !== DEF.ROOM.ID_INVALID) {
      room = this.getRoomByRid(rid);
    } else {
      room = this.getRoomByUid(uid);
    }

    if (!!room) {
      room.onPlayerAction(uid, action);
    }
  }

  public getAllRooms() {
    const rooms = new Array<Room>();
    for (let key in this._roomMap) {
      rooms.push(this._roomMap[key]);
    }
    return rooms;
  }

  public getRoomByRid(rid: number) {
    return this._roomMap[rid];
  }

  public getRoomByUid(uid: number) {
    for (let key in this._roomMap) {
      const room = this._roomMap[key];
      const players = room.playerManager.getAllPlayers();
      for (let i = 0; i < players.length; i++) {
        if (players[i].uid === uid) {
          return room;
        }
      }
    }
    return null;
  }

  public getAllPlayers(rid: number) {
    const room = this._roomMap[rid];
    return room.playerManager.getAllPlayers();
  }

  public getPlayer(uid: number) {
    for (let key in this._roomMap) {
      const room = this._roomMap[key];
      const players = room.playerManager.getAllPlayers();
      for (let i = 0; i < players.length; i++) {
        if (players[i].uid === uid) {
          return players[i];
        }
      }
    }
    return null;
  }








}

export default RoomManager.getInstance();

